package com.bulletelc.apollopre.module.main.center.invoice.list;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.InvoiceBean;
import com.bulletelc.apollopre.entity.InvoiceSelectBean;
import com.bulletelc.apollopre.framework.MVPBaseActivity;
import com.bulletelc.apollopre.module.main.center.invoice.edit.EditInvoiceActivity;
import com.bulletelc.apollopre.utils.ToastUtil;
import com.bulletelc.apollopre.widget.FoolBar;

import java.util.ArrayList;
import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/29
 * desc  : 发票界面
 */

public class InvoiceActivity extends MVPBaseActivity<InvoiceContract.View, InvoicePresenter> implements InvoiceContract.View,
        View.OnClickListener, InvoiceAdapter.OnCheckboxClickListener, InvoiceAdapter.OnInvoiceEditClickListener,
        InvoiceAdapter.OnDelClickListener, InvoiceAdapter.OnCardViewClickListener, FoolBar.ClickCallback {

    // views
    private RecyclerView recyclerView;
    private TextView tvNoInvoice;
    private TextView tvAddInvoice;
    private FoolBar flBar;
    // data
    private InvoiceSelectBean invoiceSelectBean;
    private InvoiceAdapter invoiceAdapter;
    private List<InvoiceBean> invoiceBeanList;
    private boolean isChoose = false; // 是否选择发票状态，默认False
    private int delPosition; //删除的发票的position

    @Override
    public int getLayoutRes() {
        return R.layout.invoice_list_activity;
    }

    @Override
    public void assignViews() {
        flBar = findView(R.id.flBar);
        flBar.setTitle(getResources().getString(R.string.center_invoice));
        recyclerView = findViewById(R.id.rv_invoice_list);
        tvNoInvoice = findView(R.id.tvNoInvoice);
        tvAddInvoice = findViewById(R.id.tvAddInvoice);
    }

    @Override
    public void setListener() {
        flBar.setClickCallback(this);
        tvAddInvoice.setOnClickListener(this);
    }
    @Override
    public void initData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            invoiceSelectBean = (InvoiceSelectBean) intent.getSerializableExtra(IntentCode.EXTRA_KEY_FROM_CLEARING);
        }
        if (invoiceSelectBean != null) {
            isChoose = true;
        }

        invoiceBeanList = new ArrayList<>();
        invoiceAdapter = new InvoiceAdapter(invoiceBeanList, isChoose);
        recyclerView.setAdapter(invoiceAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getApplication()));

        invoiceAdapter.setOnInvoiceEditListener(this);
        invoiceAdapter.setOnInvoiceRemoveListener(this);
        invoiceAdapter.setOnCheckboxClickListener(this);
        invoiceAdapter.setOnCardViewClickListener(this);
        presenter.getInvoiceList();
    }
    /**
     * 删除发票
     *
     * @param position
     */
    private void delInvoice(final int position) {
        this.delPosition = position;
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage("确定删除地址吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        presenter.delInvoice(invoiceBeanList.get(position).getId(), position);
                    }
                })
                .setNegativeButton("取消", null)
                .create();
        dialog.show();
    }
    @Override
    public void onSuccessInvoiceList(List<InvoiceBean> invoiceBeans) {
        Log.i("invoice11111", invoiceBeans.toString());
        if (invoiceBeans != null && invoiceBeans.size() > 0) {
            invoiceBeanList.clear();
            invoiceBeanList.addAll(invoiceBeans);
            invoiceAdapter.notifyDataSetChanged();

            recyclerView.setVisibility(View.VISIBLE);
            tvNoInvoice.setVisibility(View.GONE);

        } else {
            recyclerView.setVisibility(View.GONE);
            tvNoInvoice.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onFailInvoiceList(String fail) {
        Toast.makeText(this, fail, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onErrorInvoiceList(String error) {
        ToastUtil.showLongToast(this, R.string.network_msg);
    }

    @Override
    public void onSuccessDelInvoice() {
        Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
        invoiceBeanList.remove(delPosition);
        if (invoiceBeanList.size() > 0) {
            invoiceAdapter.notifyDataSetChanged();
        } else {
            recyclerView.setVisibility(View.GONE);
            tvNoInvoice.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onFailDelInvoice(String fail) {
        Toast.makeText(this, fail, Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onErrorDelInvoice(String error) {
        ToastUtil.showLongToast(this, R.string.network_msg);
    }

    @Override
    public void onSuccessSetDefault() {
        presenter.getInvoiceList();
    }

    @Override
    public void onFailSetDefault(String fail) {
        Toast.makeText(this, fail, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onErrorSetDefault(String error) {
        ToastUtil.showLongToast(this, R.string.network_msg);
    }

    @Override
    protected InvoicePresenter createPresenter() {
        return new InvoicePresenter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvAddInvoice:
                Intent intent = new Intent(getApplicationContext(), EditInvoiceActivity.class);
                startActivityForResult(intent, IntentCode.REQUEST_CODE_INVOICE_LIST_TO_EDIT);
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case IntentCode.REQUEST_CODE_INVOICE_LIST_TO_EDIT:
                if (resultCode == IntentCode.RESULT_CODE_EDIT_INVOICE_RETURN_LIST) {
                    presenter.getInvoiceList();
                }
                break;
        }

    }
    @Override
    public void onCheckboxClick(int position) {
        presenter.setDefault(invoiceBeanList.get(position).getId());
    }

    @Override
    public void onDelClick(View view, int position) {
        delInvoice(position);
    }

    @Override
    public void onEditClick(View view, int position) {
        Intent intent = new Intent(getApplicationContext(), EditInvoiceActivity.class);
        intent.putExtra(IntentCode.EXTRA_KEY_INVOICE_ID, invoiceBeanList.get(position).getId());
        startActivityForResult(intent, IntentCode.REQUEST_CODE_INVOICE_LIST_TO_EDIT);
    }

    @Override
    public void onCardViewClick(View view, int position) {
        invoiceSelectBean.setId(invoiceBeanList.get(position).getId());
        invoiceSelectBean.setName(invoiceBeanList.get(position).getCompany_name());
        invoiceSelectBean.setType(invoiceBeanList.get(position).getType());
        Intent intent = new Intent();

        intent.putExtra(IntentCode.EXTRA_KEY_INVOICE_SELECT_BEAN, invoiceSelectBean);
        setResult(IntentCode.RESULT_CODE_INVOICE_RETURN_CLEARING, intent);
        finish();
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }
}
